package com.xpgk.mvc.collection.binShopService;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.jfinal.aop.Before;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.bentryRegister.BentryRegisterService;
import com.xpgk.mvc.bentryRegister.Customer;
import com.xpgk.mvc.bentryRegister.CustomerAgreement;
import com.xpgk.mvc.bentryRegister.CustomerDetail;
import com.xpgk.mvc.bentryRegister.CustomerHabit;
import com.xpgk.mvc.bentryRegister.CustomerProduct;
import com.xpgk.mvc.binShopService.BinShopServiceService;
import com.xpgk.mvc.binShopService.CustomerService;
import com.xpgk.mvc.binShopService.CustomerServiceMonthNurse;
import com.xpgk.mvc.binShopService.CustomerServiceNurse;
import com.xpgk.mvc.binShopService.ServicePartSize;
import com.xpgk.mvc.binShopService.ServiceShaping;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.productSales.ProductSalesOrder;
import com.xpgk.mvc.productSet.Product;
import com.xpgk.mvc.statistic.dailyTotalStatistic.DailyNursingTotal;
import com.xpgk.mvc.statistic.monthTotalStatistic.MonthTotal;
import com.xpgk.mvc.statistic.yearTotalStatistic.YearTotal;
import com.xpgk.mvc.user.User;
import com.xpgk.tools.ToolDateTime;

import sun.reflect.generics.tree.FloatSignature;

public class BinShopColServiceController extends BaseController {
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(BinShopColServiceController.class);

	private BinShopColServiceService binShopColServiceService;
	private BinShopServiceService binShopServiceService;
	private BentryRegisterService bentryRegisterService;

	public void index() {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("column", Product.column_typeIds);
		String sql = getSqlByBeetl(Product.sqlId_column, param);
		setAttr("plaster", Product.dao.find(sql, Code.proYt_ids));
		setAttr("organids", getCUserOrganIds());
		String date = getPara("date");
		if(date==null||date.equals("")){
			setAttr("date", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		}else{
			setAttr("date",date);
		}
		render("BinShopService.html");
	}
	
	
	
	
	
	
	/**
	 * 根据会员编号,手机号,姓名查出会员信息
	 * 
	 */
	public void getCusByHybhbl() {
		String hybh = getPara("hybh");
		String name = getPara("name");
		String phone = getPara("phone");
		String date = getPara("_easyui_textbox_input38");
		// 获取当前登录机构id
		String orgid = getCUserOrganIds();
		Map<String, Object> mm = bentryRegisterService.getCusByHybh(hybh, name, phone, orgid);
		if(date!=null && !date.equals("")){
		
		//Map<String, Object> mm = bentryRegisterService.getCusByHybh(hybh, name, phone, orgid);
		Date  cus = new Date();
		 CustomerServiceNurse customerServiceNurse  = null;
		if(mm.get("customer")!=null){
		  cus =((Customer) mm.get("customer")).getDate("registtime");
		  String sql = "SELECT * FROM sb_customer_service_nurse WHERE YearMon = ? AND customerID = ?";
		 customerServiceNurse = CustomerServiceNurse.dao.findFirst(sql,date,((Customer) mm.get("customer")).getStr(Customer.column_ids));
		  
		}
		SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM");
		mm.put("date", sdf.format(cus));
		mm.put("flag", 1);
		mm.put("customerServiceNurse",customerServiceNurse);
		}else{
			
			mm.put("flag", 0);
		}
		renderJson(mm);
	}
	
	
	/**
	 * 根据会员编号,手机号,姓名查出会员信息
	 * 
	 */
	public void getCusByHybh() {
		String hybh = getPara("hybh");
		String name = getPara("name");
		String phone = getPara("phone");
		String gxsj = getPara("temporary.gxsj");
		// 获取当前登录机构id
		String orgid = getCUserOrganIds();
		Map<String, Object> mm = bentryRegisterService.getCusByHybh(hybh, name, phone, orgid);
		Record  cd =(Record) mm.get("customerDetail");
		Record cs = Db.findFirst("select a.*,sys_getUserName(a.plasterServier) sf  ,sys_getUserName(b.yb) yb , sys_getUserName(b.dt) dt, sys_getUserName(b.xt) xt, sys_getUserName(b.gb) gb  ,sys_getUserName(b.bb) bb  ,sys_getUserName(b.kb) kb ,sys_getUserName(b.zfr) zfr ,sys_getUserName(b.yfr) yfr from sb_customer_service  a left join  sb_customer_service_shaping  b on a.ids=b.serviceIds  where date_format(a.startTime,'%Y-%m-%d')=? and  a.customerID = ? and a.dataStatus in ('1','2')",gxsj,cd.getStr(CustomerDetail.column_customerIds));
		mm.put("customerService", cs);
		
		renderJson(mm);
	}
	
	
	
	@Before(Tx.class)
	public void updateOrsaveSerbl() {
		try {
			boolean flag = false;
			Customer cus = getModel(Customer.class, "customer");
			CustomerServiceNurse cussn = getModel(CustomerServiceNurse.class, "customerServiceNurse");
			flag = binShopColServiceService.updateOrsaveSerbl(cus, cussn, getCUserIds(), getCUserOrganIds());
			renderJson(flag);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	@Before(Tx.class)
	public void updateOrsaveSer() throws ParseException {
	try {
		boolean flag = false; 
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟 
		String gxsj = getPara("temporary.gxsj");
		String cid = getPara("customer.ids");
		String weight = getPara("customerService.weight");
		String plasterCount = getPara("customerService.plasterCount");
		String sf = getId(getPara("sf").replace("-1", "").trim(),getCUserOrganIds());
		String yb = getId(getPara("yb").replace("-1", "").trim(),getCUserOrganIds());
		String dt = getId(getPara("dt").replace("-1", "").trim(),getCUserOrganIds());
		String xt = getId(getPara("xt").replace("-1", "").trim(),getCUserOrganIds());
		String gb = getId(getPara("gb").replace("-1", "").trim(),getCUserOrganIds());
		String bb =null;
		String kb =null;
		String zfr =null;
		String yfr =null;
		if(getPara("bb")!=null&&!getPara("bb").equals("")){
			bb= getId(getPara("bb").replace("-1", "").trim(),getCUserOrganIds());
		}
		if(getPara("kb")!=null&&!getPara("kb").equals("")){
			 kb = getId(getPara("kb").replace("-1", "").trim(),getCUserOrganIds());
		}
		if(getPara("zfr")!=null&&!getPara("zfr").equals("")){
			 zfr = getId(getPara("zfr").replace("-1", "").trim(),getCUserOrganIds());
		}
		if(getPara("yfr")!=null&&!getPara("yfr").equals("")){
			yfr = getId(getPara("yfr").replace("-1", "").trim(),getCUserOrganIds());
		}
		
		
		
		
		String bz = getPara("customerService.bz");
		
		//旧记录
		CustomerService cd = CustomerService.dao.findFirst("select *  from sb_customer_service  where DATE_FORMAT(startTime, '%Y-%m-%d')  =? and  customerID = ? and dataStatus in ('1','2')",gxsj,cid);
		//该顾客所有服务记录
		List<CustomerService> csList = CustomerService.dao.find("SELECT * FROM sb_customer_service WHERE customerID = ? and dataStatus in ('1','2')  ORDER BY weight ",cid);
		CustomerDetail cdet = CustomerDetail.dao.findFirst("SELECT * FROM sb_customer_detail WHERE customerIds = ?  and dataStatus in ('1','2')",cid);
		boolean isXgk = ToolDateTime.format(cdet.getDate(CustomerDetail.column_firstDate), ToolDateTime.pattern_y).equals(gxsj.substring(0,4));
		boolean isdndyXgk = ToolDateTime.format(cdet.getDate(CustomerDetail.column_firstDate), ToolDateTime.pattern_ym).equals(gxsj.substring(0,7));
		Float standardWeight = Float.parseFloat(cdet.getStr(CustomerDetail.column_standardWeight));	
		if (cd != null) {
		/*	if(csList!=null && csList.size()>0 && csList.get(0).getFloat(CustomerService.column_weight)!=null){
				float minWeight = csList.get(0).getFloat(CustomerService.column_weight);
				if(minWeight<standardWeight){
					Date startTime = csList.get(0).getDate(CustomerService.column_startTime);
					String sql = "SELECT * FROM sb_customer_service WHERE startTime > ? and dataStatus in ('1','2') ";
					List<CustomerService> cService = CustomerService.dao.find(sql,startTime);
					for(CustomerService cs : cService){
						cs.set(CustomerService.column_isGg, "1");
						cdet.set(CustomerDetail.column_customerType, "2");
						flag=cs.update();
						flag=cdet.update();
					}
				}else{
					for(CustomerService cs:csList){
						cs.set(CustomerService.column_isGg, "0");
						cdet.set(CustomerDetail.column_customerType, "0");
						flag=cs.update();
						flag=cdet.update();
					}
					
				}
			}*/
			
			float oldWeight = cd.getFloat(CustomerService.column_weight);
				//更新日护理综合查询表
				DailyNursingTotal  dnt = DailyNursingTotal.dao.getDnt(getCUserOrganIds(), sdf.parse(gxsj));
				if(dnt != null)
				{
					if(Float.parseFloat(weight)!=oldWeight){
						float xgtzcz = oldWeight-Float.parseFloat(weight);//修改体重的差值
					
					/*if(cd.getFloat(CustomerService.column_weight) < standardWeight  && Float.parseFloat(weight) > standardWeight)//当时录入为达标，补录修改体重为不达标的情况
					{
						dnt.set(DailyNursingTotal.column_dbrs, dnt.getInt(DailyNursingTotal.column_dbrs)-1);
						if(!dnt.update())
						{
							log.debug("更新日护理综合查询表达标人数失败！！");
							throw new RuntimeException("更新日护理综合查询表达标人数失败!");
						}
					}*/
					/*if(cd.getFloat(CustomerService.column_weight) > standardWeight  && Float.parseFloat(weight) < standardWeight)//当时录入为不达标，补录修改体重为达标的情况
					{
						dnt.set(DailyNursingTotal.column_dbrs, dnt.getInt(DailyNursingTotal.column_dbrs)+1);
						if(!dnt.update())
						{
							log.debug("更新日护理综合查询表达标人数失败！！");
							throw new RuntimeException("更新日护理综合查询表达标人数失败!");
						}
					}*/
					
					//掉秤人数
					String  ssql = "SELECT * FROM sb_customer_service  WHERE  organids = ? AND  customerID = ? AND  dataStatus in ('1','2') and  DATE_FORMAT(startTime, '%Y-%m-%d') < ? ORDER BY  startTime DESC LIMIT 0,1";//补录日期前一次服务
					String xsql = "SELECT * FROM sb_customer_service  WHERE  organids = ? AND  customerID = ? AND   dataStatus in ('1','2') and  DATE_FORMAT(startTime, '%Y-%m-%d') > ? ORDER BY  startTime LIMIT 0,1";//补录日期后一次服务
					CustomerService  scs = CustomerService.dao.findFirst(ssql,getCUserOrganIds(),cid,gxsj);
					CustomerService  xcs = CustomerService.dao.findFirst(xsql,getCUserOrganIds(),cid,gxsj);
					

						
						float js = 0f;
						float oldjs = 0f;
						if(scs!=null)//修改日期前一次服务
						{	
							float xjs =Float.parseFloat(weight) - scs.getFloat(CustomerService.column_weight);//新修改的本次体重-上次体重
							js = Float.parseFloat(new DecimalFormat("#.0").format(xjs));
							float oldxjs =oldWeight - scs.getFloat(CustomerService.column_weight);//旧体重-上次体重
							oldjs = Float.parseFloat(new DecimalFormat("#.0").format(oldxjs));
							
						}else
						{	
							if(null != cdet.getStr(CustomerDetail.column_customerType) && cdet.getStr(CustomerDetail.column_customerType).equals("0"))
							{
								float xjs = Float.parseFloat(weight) - cdet.getFloat(CustomerDetail.column_firstWeight);
								js = Float.parseFloat(new DecimalFormat("#.0").format(xjs));
								float oldxjs =oldWeight - cdet.getFloat(CustomerDetail.column_firstWeight);
								oldjs = Float.parseFloat(new DecimalFormat("#.0").format(oldxjs));
							}
						}
						if(js>0.00 )//新纪录为涨称
						{
							if(oldjs<0.00){//旧记录为掉秤
								dnt.set(DailyNursingTotal.column_dcrs, dnt.getInt(DailyNursingTotal.column_dcrs) - 1)
								.set(DailyNursingTotal.column_dcjs, dnt.getDouble(DailyNursingTotal.column_dcjs)- (-oldjs))
								.set(DailyNursingTotal.column_zcrs, dnt.getInt(DailyNursingTotal.column_zcrs)+1)//涨称人数
								.set(DailyNursingTotal.column_zcjs, dnt.getDouble(DailyNursingTotal.column_zcjs)+js);
								if(isXgk){
								dnt.set(DailyNursingTotal.column_xgkdcrs, dnt.getInt(DailyNursingTotal.column_xgkdcrs)-1)
								.set(DailyNursingTotal.column_xgkdcjs, dnt.getDouble(DailyNursingTotal.column_xgkdcjs)-(-oldjs))
								.set(DailyNursingTotal.column_xgkzcrs, dnt.getInt(DailyNursingTotal.column_xgkzcrs)+1)//涨称人数
								.set(DailyNursingTotal.column_xgkzcjs, dnt.getDouble(DailyNursingTotal.column_xgkzcjs)+js);
								}
							}
							
							if(oldjs>0.00){//旧记录为涨称
								dnt.set(DailyNursingTotal.column_zcjs, dnt.getDouble(DailyNursingTotal.column_zcjs)+(-xgtzcz));
								if(isXgk){
									dnt.set(DailyNursingTotal.column_xgkzcjs, dnt.getDouble(DailyNursingTotal.column_xgkzcjs)+(-xgtzcz));
								}
							}
							
							if(oldjs == 0){
								dnt.set(DailyNursingTotal.column_pcrs, dnt.getInt(DailyNursingTotal.column_pcrs) - 1)
								
								.set(DailyNursingTotal.column_zcrs, dnt.getInt(DailyNursingTotal.column_zcrs)+1)//涨称人数
								.set(DailyNursingTotal.column_zcjs, dnt.getDouble(DailyNursingTotal.column_zcjs)+js);
								if(isXgk){
									dnt.set(DailyNursingTotal.column_xgkzcrs, dnt.getInt(DailyNursingTotal.column_xgkzcrs)+1)//涨称人数
									.set(DailyNursingTotal.column_xgkzcjs, dnt.getDouble(DailyNursingTotal.column_xgkzcjs)+js);
								}
							}
							
						}
						if(js<0.00 )//新纪录为掉秤
						{
							if( oldjs>0.00){//旧记录为涨称
								dnt.set(DailyNursingTotal.column_dcrs, dnt.getInt(DailyNursingTotal.column_dcrs)+1)//掉称人数
								.set(DailyNursingTotal.column_dcjs, dnt.getDouble(DailyNursingTotal.column_dcjs)+(-js))
								.set(DailyNursingTotal.column_zcrs, dnt.getInt(DailyNursingTotal.column_zcrs)-1)//涨称人数
								.set(DailyNursingTotal.column_zcjs, dnt.getDouble(DailyNursingTotal.column_zcjs)-oldjs);
								if(isXgk){
									dnt.set(DailyNursingTotal.column_xgkdcrs, dnt.getInt(DailyNursingTotal.column_xgkdcrs)+1)
									.set(DailyNursingTotal.column_xgkdcjs, dnt.getDouble(DailyNursingTotal.column_xgkdcjs)+(-js))
									.set(DailyNursingTotal.column_xgkzcrs, dnt.getInt(DailyNursingTotal.column_xgkzcrs)-1)//涨称人数
									.set(DailyNursingTotal.column_xgkzcjs, dnt.getDouble(DailyNursingTotal.column_xgkzcjs)-oldjs);
								}
							}
							
							if( oldjs<0.00){//旧记录为掉秤
								dnt.set(DailyNursingTotal.column_dcjs, dnt.getDouble(DailyNursingTotal.column_dcjs)+xgtzcz);
								if(isXgk){
									dnt.set(DailyNursingTotal.column_xgkdcjs, dnt.getDouble(DailyNursingTotal.column_xgkdcjs)+xgtzcz);
								}
							}
							
							if(oldjs == 0){
								dnt.set(DailyNursingTotal.column_pcrs, dnt.getInt(DailyNursingTotal.column_pcrs) - 1)
								.set(DailyNursingTotal.column_dcrs, dnt.getInt(DailyNursingTotal.column_dcrs)+1)//涨称人数
								.set(DailyNursingTotal.column_dcjs, dnt.getDouble(DailyNursingTotal.column_dcjs)+(-js));
								
								if(isXgk){
									dnt.set(DailyNursingTotal.column_xgkdcrs, dnt.getInt(DailyNursingTotal.column_xgkdcrs)+1)
									.set(DailyNursingTotal.column_xgkdcjs, dnt.getDouble(DailyNursingTotal.column_xgkdcjs)+(-js));
									}
							}
							
						}
						
						
						if(js==0.00)//新纪录为平称
						{
							if(oldjs>0.00){//旧记录为涨称
								dnt.set(DailyNursingTotal.column_pcrs, dnt.getInt(DailyNursingTotal.column_pcrs)+1)//平称人数
									.set(DailyNursingTotal.column_zcrs, dnt.getInt(DailyNursingTotal.column_zcrs)-1)//涨称人数
									.set(DailyNursingTotal.column_zcjs, dnt.getDouble(DailyNursingTotal.column_zcjs)-oldjs);
								if(isXgk){
									dnt.set(DailyNursingTotal.column_xgkzcrs, dnt.getInt(DailyNursingTotal.column_xgkzcrs)-1)//涨称人数
									.set(DailyNursingTotal.column_xgkzcjs, dnt.getDouble(DailyNursingTotal.column_xgkzcjs)-oldjs);
								}
							}
							if(oldjs<0.00){
								dnt.set(DailyNursingTotal.column_pcrs, dnt.getInt(DailyNursingTotal.column_pcrs)+1)//平称人数
								.set(DailyNursingTotal.column_dcrs, dnt.getInt(DailyNursingTotal.column_dcrs)-1)//涨称人数
								.set(DailyNursingTotal.column_dcjs, dnt.getDouble(DailyNursingTotal.column_dcjs)- (-oldjs));
								if(isXgk){
									dnt.set(DailyNursingTotal.column_xgkdcrs, dnt.getInt(DailyNursingTotal.column_xgkdcrs)-1)//涨称人数
									.set(DailyNursingTotal.column_xgkdcjs, dnt.getDouble(DailyNursingTotal.column_xgkdcjs)- (-oldjs));
								}
							}
							
						}
						
						
						double  dcbl = ((double)dnt.getInt(DailyNursingTotal.column_dcrs))/((double)dnt.getInt(DailyNursingTotal.column_sddrs));
						dnt.set(DailyNursingTotal.column_dcrszb,new DecimalFormat("#.00").format(dcbl*100));
						if(xcs != null)//修改日期下一次服务
						{	
							float hjs = xcs.getFloat(CustomerService.column_weight) - Float.parseFloat(weight);
							float xjs = Float.parseFloat(new DecimalFormat("#.0").format(hjs));//修改日期下一次服务-修改体重
							float sjs = Float.parseFloat(new DecimalFormat("#.0").format(0f));
							sjs = xcs.getFloat(CustomerService.column_weight) - cd.getFloat(CustomerService.column_weight);//原记录的下一次服务减去原记录
							
							DailyNursingTotal  xdnt = DailyNursingTotal.dao.getDnt(getCUserOrganIds(),xcs.getDate(CustomerService.column_startTime));//修改服务的下一次服务
							if(xdnt != null)
							{
								if(xjs > 0 && sjs <0)//新纪录为涨称,旧记录为掉秤，
								{
									xdnt.set(DailyNursingTotal.column_dcrs, xdnt.getInt(DailyNursingTotal.column_dcrs) - 1)
										.set(DailyNursingTotal.column_dcjs, xdnt.getDouble(DailyNursingTotal.column_dcjs)- (-sjs))
										.set(DailyNursingTotal.column_zcrs, xdnt.getInt(DailyNursingTotal.column_zcrs) + 1)
										.set(DailyNursingTotal.column_zcjs, xdnt.getDouble(DailyNursingTotal.column_zcjs) + xjs);
									if(isXgk){
										xdnt.set(DailyNursingTotal.column_xgkdcrs, xdnt.getInt(DailyNursingTotal.column_xgkdcrs)-1)
										.set(DailyNursingTotal.column_xgkdcjs, xdnt.getDouble(DailyNursingTotal.column_xgkdcjs)-(-sjs))
										.set(DailyNursingTotal.column_xgkzcrs, xdnt.getInt(DailyNursingTotal.column_xgkzcrs) + 1)
										.set(DailyNursingTotal.column_xgkzcjs, xdnt.getDouble(DailyNursingTotal.column_xgkzcjs) + xjs);
									}
									double  xdcbl = ((double)xdnt.getInt(DailyNursingTotal.column_dcrs))/((double)xdnt.getInt(DailyNursingTotal.column_sddrs));
									xdnt.set(DailyNursingTotal.column_dcrszb,new DecimalFormat("#.00").format(xdcbl*100));
									if (!xdnt.update()) {
										log.debug("更新日综合查询表失败");
										throw new RuntimeException("更新日综合查询表失败!");
									}
								}
								if(xjs < 0 && sjs > 0)//补录为掉称,旧记录为涨秤，
								{
									xdnt.set(DailyNursingTotal.column_dcrs, xdnt.getInt(DailyNursingTotal.column_dcrs) + 1)
									.set(DailyNursingTotal.column_dcjs, xdnt.getDouble(DailyNursingTotal.column_dcjs) + (-xjs))
									.set(DailyNursingTotal.column_zcrs, xdnt.getInt(DailyNursingTotal.column_zcrs) - 1)
									.set(DailyNursingTotal.column_zcjs, xdnt.getDouble(DailyNursingTotal.column_zcjs) - sjs);
									if(isXgk){
										xdnt.set(DailyNursingTotal.column_xgkdcrs, xdnt.getInt(DailyNursingTotal.column_xgkdcrs)+1)
										.set(DailyNursingTotal.column_xgkdcjs, xdnt.getDouble(DailyNursingTotal.column_xgkdcjs)+(-xjs))
										.set(DailyNursingTotal.column_xgkzcrs, xdnt.getInt(DailyNursingTotal.column_xgkzcrs) - 1)
										.set(DailyNursingTotal.column_xgkzcjs, xdnt.getDouble(DailyNursingTotal.column_xgkzcjs) - sjs);
									}
									double  xdcbl = ((double)xdnt.getInt(DailyNursingTotal.column_dcrs))/((double)xdnt.getInt(DailyNursingTotal.column_sddrs));
									xdnt.set(DailyNursingTotal.column_dcrszb,new DecimalFormat("#.00").format(xdcbl*100));
									if (!xdnt.update()) {
										log.debug("更新日综合查询表失败");
										throw new RuntimeException("更新日综合查询表失败!");
									}
								}
								
								
								if(xjs < 0 && sjs < 0)//补录、旧记录为掉称
								{
									xdnt.set(DailyNursingTotal.column_dcjs, xdnt.getDouble(DailyNursingTotal.column_dcjs) +sjs+ (-xjs));
									if(isXgk){
										xdnt.set(DailyNursingTotal.column_xgkdcjs, xdnt.getDouble(DailyNursingTotal.column_xgkdcjs) +sjs+ (-xjs));
									}
									double  xdcbl = ((double)xdnt.getInt(DailyNursingTotal.column_dcrs))/((double)xdnt.getInt(DailyNursingTotal.column_sddrs));
									xdnt.set(DailyNursingTotal.column_dcrszb,new DecimalFormat("#.00").format(xdcbl*100));
									if (!xdnt.update()) {
										log.debug("更新日综合查询表失败");
										throw new RuntimeException("更新日综合查询表失败!");
									}
								}
								
								if(xjs > 0 && sjs > 0)//补录、旧记录为涨称
								{
									xdnt.set(DailyNursingTotal.column_zcjs, xdnt.getDouble(DailyNursingTotal.column_zcjs) - sjs+xjs);
									
									double  xdcbl = ((double)xdnt.getInt(DailyNursingTotal.column_dcrs))/((double)xdnt.getInt(DailyNursingTotal.column_sddrs));
									xdnt.set(DailyNursingTotal.column_dcrszb,new DecimalFormat("#.00").format(xdcbl*100));
									if (!xdnt.update()) {
										log.debug("更新日综合查询表失败");
										throw new RuntimeException("更新日综合查询表失败!");
									}
								}
								
								if (xjs == 0){
									if(sjs>0){
										xdnt.set(DailyNursingTotal.column_zcjs, xdnt.getDouble(DailyNursingTotal.column_zcjs) - sjs)
											.set(DailyNursingTotal.column_zcrs, xdnt.getInt(DailyNursingTotal.column_zcrs) - 1)
											.set(DailyNursingTotal.column_pcrs, xdnt.getInt(DailyNursingTotal.column_pcrs) + 1);
										if(isXgk){
											xdnt.set(DailyNursingTotal.column_xgkzcjs, xdnt.getDouble(DailyNursingTotal.column_xgkzcjs) - sjs)
											.set(DailyNursingTotal.column_xgkzcrs, xdnt.getInt(DailyNursingTotal.column_xgkzcrs) - 1);
										}
										double  xdcbl = ((double)xdnt.getInt(DailyNursingTotal.column_dcrs))/((double)xdnt.getInt(DailyNursingTotal.column_sddrs));
										xdnt.set(DailyNursingTotal.column_dcrszb,new DecimalFormat("#.00").format(xdcbl*100));
										if (!xdnt.update()) {
											log.debug("更新日综合查询表失败");
											throw new RuntimeException("更新日综合查询表失败!");
										}
									}
									
									if(sjs<0){
										xdnt.set(DailyNursingTotal.column_dcjs, xdnt.getDouble(DailyNursingTotal.column_dcjs) - (-sjs))
											.set(DailyNursingTotal.column_dcrs, xdnt.getInt(DailyNursingTotal.column_dcrs) - 1)
											.set(DailyNursingTotal.column_pcrs, xdnt.getInt(DailyNursingTotal.column_pcrs) + 1);
										
										if(isXgk){
											xdnt.set(DailyNursingTotal.column_xgkdcrs, xdnt.getInt(DailyNursingTotal.column_xgkdcrs)-1)
											.set(DailyNursingTotal.column_xgkdcjs, xdnt.getDouble(DailyNursingTotal.column_xgkdcjs) - (-sjs));
										}
										double  xdcbl = ((double)xdnt.getInt(DailyNursingTotal.column_dcrs))/((double)xdnt.getInt(DailyNursingTotal.column_sddrs));
										xdnt.set(DailyNursingTotal.column_dcrszb,new DecimalFormat("#.00").format(xdcbl*100));
										if (!xdnt.update()) {
											log.debug("更新日综合查询表失败");
											throw new RuntimeException("更新日综合查询表失败!");
										}
									}
								}
							}	
						}
						if (!dnt.update()) {
							log.debug("更新日综合查询表失败");
							throw new RuntimeException("更新日综合查询表失败!");
						}
					}
				}
				
			
				cd.set(CustomerService.column_weight, weight).set(CustomerService.column_plasterCount, plasterCount)
						.set(CustomerService.column_plasterServier, sf).set(CustomerService.column_bz, bz)
						.set(CustomerService.column_updateId, getCUserIds())
						.set(CustomerService.column_updateDate, new Date());
				
				
				if (StringUtils.isNotBlank(sf)) {
					cd.set(CustomerService.column_isPlaster, "1");
				}
				if (StringUtils.isNotBlank(yb) || StringUtils.isNotBlank(dt) || StringUtils.isNotBlank(xt)|| StringUtils.isNotBlank(gb)) {
					cd.set(CustomerService.column_isShaping, "1");
				}
				flag= cd.update();

				
				
				

			

				String sid = cd.getStr(CustomerService.column_ids);
				// 保存精护部位
				if (StringUtils.isNotBlank(yb) || StringUtils.isNotBlank(dt) || StringUtils.isNotBlank(xt)|| StringUtils.isNotBlank(gb)|| StringUtils.isNotBlank(bb)|| StringUtils.isNotBlank(kb)|| StringUtils.isNotBlank(zfr)|| StringUtils.isNotBlank(yfr)) {
					ServiceShaping ss = ServiceShaping.dao.findFirst("select *  from   sb_customer_service_shaping   where   serviceIds  = ?  and dataStatus in ('1','2')", sid);
					if (ss != null) {
						if (StringUtils.isNotBlank(yb)) {
							ss.set(ServiceShaping.column_yb, yb);
						}
						if (StringUtils.isNotBlank(dt)) {
							ss.set(ServiceShaping.column_dt, dt);
						}
						if (StringUtils.isNotBlank(xt)) {
							ss.set(ServiceShaping.column_xt, xt);
						}
						if (StringUtils.isNotBlank(gb)) {
							ss.set(ServiceShaping.column_gb, gb);
						}
						if (StringUtils.isNotBlank(yb)) {
							ss.set(ServiceShaping.column_bb, bb);
						}
						if (StringUtils.isNotBlank(dt)) {
							ss.set(ServiceShaping.column_kb, kb);
						}
						if (StringUtils.isNotBlank(xt)) {
							ss.set(ServiceShaping.column_zfr, zfr);
						}
						if (StringUtils.isNotBlank(gb)) {
							ss.set(ServiceShaping.column_yfr, yfr);
						}
						flag = ss.update();
						
					} else {
						ServiceShaping cs = new ServiceShaping();
						cs.set(ServiceShaping.column_customerIds, cid);
						cs.set(ServiceShaping.column_createDate, new Date());
						cs.set(ServiceShaping.column_createId, getCUserIds());
						cs.set(ServiceShaping.column_organIds, getCUserOrganIds());
						cs.set(ServiceShaping.column_serviceIds, sid);
						cs.set(ServiceShaping.column_dataStatus, 2);
						if (StringUtils.isNotBlank(yb)) {
							cs.set(ServiceShaping.column_yb, yb);
						}
						if (StringUtils.isNotBlank(dt)) {
							cs.set(ServiceShaping.column_dt, dt);
						}
						if (StringUtils.isNotBlank(xt)) {
							cs.set(ServiceShaping.column_xt, xt);
						}
						if (StringUtils.isNotBlank(gb)) {
							cs.set(ServiceShaping.column_gb, gb);
						}
						
						if (StringUtils.isNotBlank(yb)) {
							cs.set(ServiceShaping.column_bb, bb);
						}
						if (StringUtils.isNotBlank(dt)) {
							cs.set(ServiceShaping.column_kb, kb);
						}
						if (StringUtils.isNotBlank(xt)) {
							cs.set(ServiceShaping.column_zfr, zfr);
						}
						if (StringUtils.isNotBlank(gb)) {
							cs.set(ServiceShaping.column_yfr, yfr);
						}
						
						flag =cs.save();
						
							//保存日综合查询使用产品人数
							if(dnt != null)
							{
								dnt.set(DailyNursingTotal.column_sycprs, dnt.getInt(DailyNursingTotal.column_sycprs)+1);
								if(isXgk){
									dnt.set(DailyNursingTotal.column_xgkjhrs, dnt.getInt(DailyNursingTotal.column_xgkjhrs)+1);
								}
								double  dcbl1 = ((double)dnt.getInt(DailyNursingTotal.column_sycprs))/((double)dnt.getInt(DailyNursingTotal.column_sddrs));
								dnt.set(DailyNursingTotal.column_sycprszb,new DecimalFormat("#.00").format(dcbl1*100));
								if(!dnt.update())
								{
									log.debug("更新日综合查询表达标人数失败！！");
									throw new RuntimeException("更新日综合查询表达标人数失败!");
								}
							}else
							{
								dnt = new DailyNursingTotal();
								dnt.set(DailyNursingTotal.column_organids, getCUserOrganIds())
									.set(DailyNursingTotal.column_date, ToolDateTime.format(sdf.parse(gxsj), ToolDateTime.pattern_ymd))
									.set(DailyNursingTotal.column_sycprs,1);
								if(isXgk){
									dnt.set(DailyNursingTotal.column_xgkjhrs, 1);
								}
								double  dcbl1 = ((double)dnt.getInt(DailyNursingTotal.column_sycprs))/((double)dnt.getInt(DailyNursingTotal.column_sddrs));
								dnt.set(DailyNursingTotal.column_sycprszb,new DecimalFormat("#.00").format(dcbl1*100));
								if(!dnt.save())
								{
									log.debug("保存日综合查询表达标人数失败！！");
									throw new RuntimeException("保存日综合查询表达标人数失败!");
								}
							}
						
						
					}
				
					
				}

				String servicesql = getSqlMy(CustomerService.sqlId_selNextWeight);
				CustomerService cs0 = CustomerService.dao.findFirst(servicesql ,gxsj,cid);
				if (cs0 != null) {
					cs0.set(CustomerService.column_lastWeight, weight);
					cs0.set(CustomerService.column_updateDate, new Date());
					cs0.set(CustomerService.column_updateId, getCUserIds());
					cs0.update();
				}
				
				
				
				servicesql = getSqlMy(CustomerService.sqlId_selMinByCus);
				CustomerService cs1 = CustomerService.dao.findFirst(servicesql, cid);
				if (cs1 != null && cs1.getFloat(CustomerService.column_weight)!=null) {
					float minWeight = cs1.getFloat(CustomerService.column_weight);
					cdet.set(CustomerDetail.column_minDate, cs1.getDate(CustomerService.column_startTime));
					cdet.set(CustomerDetail.column_minWeight,minWeight);
					cdet.set(CustomerDetail.column_updateDate, new Date());
					cdet.set(CustomerDetail.column_updateId, getCUserIds());
					flag =cdet.update();
						
						if(minWeight<=standardWeight){
							Date startTime = csList.get(0).getDate(CustomerService.column_startTime);
							String sql = "SELECT * FROM sb_customer_service WHERE startTime >= ? and  customerID = ? and dataStatus in ('1','2') ";
							List<CustomerService> cService = CustomerService.dao.find(sql,startTime,cid);
							for(CustomerService cs : cService){
								cs.set(CustomerService.column_isGg, "1");
								cdet.set(CustomerDetail.column_customerType, "2");
								flag=cs.update();
								flag=cdet.update();
							}
						}else{
							for(CustomerService cs:csList){
								cs.set(CustomerService.column_isGg, "0");
								cdet.set(CustomerDetail.column_customerType, "0");
								flag=cs.update();
								flag=cdet.update();
							}
							
						}
					
				}
				
				
				servicesql = getSqlMy(CustomerService.sqlId_selNowByCus);
				CustomerService cs2 = CustomerService.dao.findFirst(servicesql, cid);
				if (cs2 != null) {
					cdet.set(CustomerDetail.column_toStoreDate, cs2.getDate(CustomerService.column_startTime));
					cdet.set(CustomerDetail.column_nowWeight, cs2.getFloat(CustomerService.column_weight));
					cdet.set(CustomerDetail.column_updateDate, new Date());
					cdet.set(CustomerDetail.column_updateId, getCUserIds());
					flag = cdet.update();
				}
				
				CustomerServiceNurse csmn = CustomerServiceNurse.dao.findFirst(" SELECT * FROM sb_customer_service_nurse WHERE customerID = ? and organids = ? and  YearMon = ?   LIMIT 1", cid,getCUserOrganIds(),gxsj.substring(0, 7));
				binShopColServiceService.saveOrUpdateNurse(csmn, CustomerService.dao.findById(cd.get("ids")), cdet,0,cid,1,isdndyXgk);
				CustomerServiceMonthNurse monthNurse = CustomerServiceMonthNurse.dao.findFirst("select * from sb_customer_service_month_nurse where customerIds = ? and organIds = ? and `year` = ? and dataStatus in ('1','2')",cid,getCUserOrganIds(),gxsj.substring(0,4));
				binShopServiceService.saveOrUpdateMonthNurse(monthNurse,CustomerService.dao.findById(cd.get("ids")),cdet,1,getCUserIds(),3);
			} else{
				flag = false;
			}
		
			
			
		setAttr("date", gxsj);
		renderJson(flag);
	} catch (Exception e) {
		e.printStackTrace();
	}
	}
	
	
	
	
	
	
	private String getId(String trim, String cUserOrganIds) {
		if(StringUtils.isNotBlank(trim)&&trim.length()<8){
			User u =  User.dao.findFirst (" select  *  from  sys_user  where  name = ?  and organizationids = ? ",trim,cUserOrganIds);
			return u.getStr("ids");
		}
		return trim;
	}


	/**
	 * 获取顾客是否支付疗程款，单疗程次数，测量尺寸日期
	 * 
	 */
	public void getInshopInfo() {
		RenderBean result = new RenderBean();
		String customerIds = getPara("customerIds");
		Customer cus = Customer.dao.findById(customerIds);
		// cus 是否购买产品，和疗程
		StringBuffer sb = new StringBuffer();
		sb.append("亲，");
		String sql1 = " SELECT * FROM sb_customer_product WHERE customerID = ?   AND  ( productID IN ('0896ce4d4970455d9184e6dd1ced6a0e','27aeb2d41d044394b9b58f6814baa4fa','4777c2c215b6408ab13c1eac4190ce46'))  ";
		List<Record> cdl = Db.find(sql1, customerIds);
		if (cdl == null || cdl.size() == 0) {
			result.getMap().put("msg",sb.append("没有购买疗程！"));
		}

		String sql0 = " SELECT * FROM sb_customer_product WHERE customerID = ?   AND  ( productID IN ('daaba791f4104ae98504a1bc421e0d8d','95e2ae17462e4d0eb1dc84bc63bc478a'))  ";
		cdl = Db.find(sql0, customerIds);
		if (cdl == null || cdl.size() == 0) {
			result.getMap().put("msg",sb.append("没有购买瘦身产品！"));
		} else {

			String sql01 = "SELECT  SUM(totalExperience)<5  sy FROM sb_customer_product WHERE      productID ='daaba791f4104ae98504a1bc421e0d8d'  and customerID = ?";
			cdl = Db.find(sql01, customerIds);
			if (cdl == null || cdl.size() == 0) {
				if (Integer.parseInt(cdl.get(0).getStr("sy")) > 0) {
					result.getMap().put("msg", sb.append("紧肤膏剩余" + cdl.get(0).getStr("sy") + "次！"));
				}
			}

			String sql02 = " SELECT  SUM(totalExperience)<5  sy FROM sb_customer_product WHERE      productID ='95e2ae17462e4d0eb1dc84bc63bc478a'  and customerID = ?";
			cdl = Db.find(sql01, customerIds);
			if (cdl == null || cdl.size() == 0) {
				if (Integer.parseInt(cdl.get(0).getStr("sy")) > 0) {
					result.getMap().put("msg", sb.append("精油剩余" + cdl.get(0).getStr("sy") + "次！"));
				}
			}
		}
		if(sb.toString().length()>5){
			result.getMap().put("msg",sb.toString());
		}
		
		
		
		
		String sql033 = " SELECT * FROM (SELECT weight-lastWeight  tz from sb_customer_service WHERE customerID = ? ORDER BY startTime DESC  LIMIT 0,1 ) a WHERE a.tz >0";
		cdl = Db.find(sql033, customerIds);
		if (cdl != null &&cdl.size() == 1 ) {
			
			 result.getMap().put("zcscount",1);
			 result.getMap().put("zcs","亲，您连续涨称"+1+"次！");
			 String sql032 = " SELECT * FROM (SELECT weight-lastWeight  tz from sb_customer_service WHERE customerID = ? ORDER BY startTime DESC  LIMIT 0,2 ) a WHERE a.tz >0";
				cdl = Db.find(sql032, customerIds);
				if (cdl != null && cdl.size() == 2 ) {
					 result.getMap().put("zcscount",2);
					 result.getMap().put("zcs","亲，您连续涨称"+2+"次！");
					 String sql031 = " SELECT * FROM (SELECT weight-lastWeight  tz from sb_customer_service WHERE customerID = ? ORDER BY startTime DESC  LIMIT 0,3 ) a WHERE a.tz >0";
						cdl = Db.find(sql031, customerIds);
						if (cdl != null && cdl.size() == 3 ) {
							 result.getMap().put("zcscount",3);
							 result.getMap().put("zcs","亲，您连续涨称"+3+"次！");
						}
				}
		}
		
		
		

		String sql2 = " select  *  from sb_customer_detail   where customerIds= ?  ";
		cdl = Db.find(sql2, customerIds);
		Record cd = cdl.get(0);// CustomerDetail

		Date startDate = cd.getDate(CustomerDetail.column_firstDate);
		if (startDate != null) {
			Date endDate = new Date();

			Calendar startCalendar = Calendar.getInstance();
			Calendar endCalendar = Calendar.getInstance();

			startCalendar.setTime(startDate);
			endCalendar.setTime(endDate);

			int day = endCalendar.get(Calendar.DAY_OF_MONTH) - startCalendar.get(Calendar.DAY_OF_MONTH);
			int month = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
			int year = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);

			if (day < 0) {
				month--;
			}

			if (month < 0) {
				month += 12;
				year--;
			}
			if (year >= 2) {
				result.getMap().put("yearflag", "0");
				result.getMap().put("yearms","亲，您首次到店日期为"+ new SimpleDateFormat("yyyy-MM-dd").format(cd.getDate(CustomerDetail.column_firstDate))+ "已经：" + year + "年" + month + "个月不可服务!");
			}
		}
		if (cd.getStr(CustomerDetail.column_customerType) != null	&& cd.getStr(CustomerDetail.column_customerType) .equals("2")) {
				result.getMap().put("weightflag", "0");
				result.getMap().put("weightms","亲，您已经成为巩固顾客，恭喜您减肥成功！");
		}
		String sql = "SELECT  ifnull(sum(arrears),0)  qkje  from   sb_product_sales_order   where  customerIds=?    and  arrears>0";
		List<ProductSalesOrder> psoList = ProductSalesOrder.dao.find(sql, customerIds);
		Double qkje = psoList.get(0).getDouble("qkje");

		if (qkje != null && qkje > 0) {
			result.getMap().put("qkjeflag", "0");
			result.getMap().put("qkjems", "亲，您有欠款未还，金额为" + qkje.toString() + "元");
		}
		// 是否支付疗程款
		String isPay = cus.getCustomerAgreement().getStr(CustomerAgreement.column_isPay);
		result.getMap().put("isPay", isPay);
		// 是否是单疗程，单疗程剩余次数
		String lcsql = getSqlMy(CustomerProduct.sqlId_selByCusAndType);
		CustomerProduct lccp = CustomerProduct.dao.findFirst(lcsql, cus.getStr(CustomerProduct.column_ids), Code.lc_ids,
				getCUserOrganIds());
		if (lccp != null && lccp.getStr("proUnits").equals(Code.dlc_units)) {
			result.getMap().put("dlcnum", lccp.getFloat("totalexperience"));
		}
		// 查询测量尺寸时间
		String sizesql = getSqlMy(ServicePartSize.sqlId_selLastSize);
		ServicePartSize sps = ServicePartSize.dao.findFirst(sizesql, customerIds, new Date());
		if (sps != null) {
			int days = ToolDateTime.getDateDaySpace(sps.getTimestamp(ServicePartSize.column_createDate), new Date());
			if (days >= 15) {
				result.getMap().put("measureSizeDay", "1");
			}
		} else {
			int days = ToolDateTime.getDateDaySpace(cus.getTimestamp(Customer.column_registtime), new Date());
			if (days >= 15) {
				result.getMap().put("measureSizeDay", "1");
			}
		}
		SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd");
		Map<String, Object> queryParam = new HashMap<String, Object>();
		queryParam.put("customerIds", customerIds);
		queryParam.put("datestr",sdf.format(new Date()));
		List<Record> rs = binShopColServiceService.find(CustomerService.sqlId_targetWei, queryParam);
		
		// 查询目标
		if (rs != null && rs.size() > 0) {
			for (Record record : rs) {
				record.set("startTime", sdf.format(record.getDate("startTime")));
				record.set("endTime", sdf.format(record.getDate("endTime")));
			}
			result.getMap().put("mbRecord", rs);
		}
		renderJson(result.getMap());
	}

	/**
	 * 查出会员拥有的服务类的产品
	 */
	public void getPartProduct() {
		String customerIds = getPara("customerIds");
		String sql = getSqlMy(CustomerProduct.sqlId_selByCusAndUseIds);
		List<CustomerProduct> cpList = CustomerProduct.dao.find(sql, customerIds, Code.cpytFw_ids);
		renderJson(cpList);
	}

	/**
	 * 获取服务部位产品使用经验值
	 * 
	 */
	public void getPartInfo() {
		String curUserIds = getCUserIds();
		String customerIds = getPara("customerIds");
		List<Map> js = binShopColServiceService.getPartInfo(customerIds, curUserIds);
		renderJson(js);
	}

	/**
	 * 获取测量部位尺寸信息
	 * 
	 */
	public void getPartSizeInfo() {
		String customerIds = getPara("customerIds");
		List<Map> js = binShopColServiceService.getPartSizeInfo(customerIds);
		renderJson(js);
	}

	/**
	 * 获取服务人员下拉框
	 * 
	 */
	public void getServicesUser() {
		// 获取当前登录机构id
		String cudid = getCUserOrganIds();
		String userSql = getSqlMy(User.sqlId_organUserList);
		List<User> uList = User.dao.find(userSql, cudid);
		renderJson(uList);
	}

	/**
	 * 根据顾客id查出生活习惯
	 * 
	 */
	public void getCusHabit() {
		String customerIds = getPara("customerIds");
		// 获取当前登录机构id
		String cudid = getCUserOrganIds();
		CustomerHabit ch = binShopColServiceService.getCusHabit(customerIds, cudid);
		setAttr("customerHabit", ch);
		render("dailyLife.html");
	}

	/**
	 * 保存服务和生活习惯
	 * 
	 */
	@Before(Tx.class)
	public void saveInfo() {
		String customerIds = getPara("customerIds");
		CustomerDetail cdet = CustomerDetail.dao.findFirst(" SELECT * FROM sb_customer_detail WHERE customerIds = ? ",
				customerIds);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String datestr = sdf.format(new Date());
		CustomerService csm = CustomerService.dao.findFirst(" SELECT * FROM sb_customer_service WHERE customerID = ? and  DATE_FORMAT(createDate,'%Y-%m-%d')  = ? and dataStatus ='1'  LIMIT 1",customerIds, datestr);
		if (csm == null) {
			String nowweight = getPara("nowweight");
			// 获取当前登录机构id
			String cudid = getCUserOrganIds();
			// 获取当前登陆人员id
			String userid = getCUserIds();
			CustomerService cs = new CustomerService();
			cs.set(CustomerService.column_customerID, customerIds);
			cs.set(CustomerService.column_startTime, new Date());
			cs.set(CustomerService.column_createDate, new Date());
			cs.set(CustomerService.column_createId, userid);
			if (StringUtils.isNotBlank(nowweight)) {
				cs.set(CustomerService.column_isGg, 0);
				if (Float.parseFloat(nowweight) - Float.parseFloat(cdet.getStr(CustomerDetail.column_standardWeight)) <= 0) {
					if(cdet.getStr(CustomerDetail.column_customerType)==null||cdet.getStr(CustomerDetail.column_customerType).length()==0){
						cs.set(CustomerService.column_isGg, 1);
						cdet.set(CustomerDetail.column_customerType, 2);
					}else{
						if(cdet.getStr(CustomerDetail.column_customerType).equals("2")){
							cs.set(CustomerService.column_isGg,1);
						}else if(cdet.getStr(CustomerDetail.column_customerType).equals("1")){
							cs.set(CustomerService.column_isGg,2);
						}else{
							cs.set(CustomerService.column_isGg, 0);
						}
					}
				} 
				cs.set(CustomerService.column_weight, nowweight);
				CustomerService csm1 = CustomerService.dao.findFirst(
						" SELECT * FROM sb_customer_service WHERE customerID = ?   ORDER BY endTime DESC  LIMIT 1",
						customerIds);
				if (csm1 == null) {
					cs.set(CustomerService.column_lastWeight, cdet.getFloat(CustomerDetail.column_firstWeight));
				} else {
					cs.set(CustomerService.column_lastWeight, csm1.getFloat("weight"));
				}
			}
			cs.set(CustomerService.column_organids, cudid);
			cs.set(CustomerService.column_dataStatus, "1");
			boolean flag = cs.save();
			if (StringUtils.isNotBlank(nowweight)) {
				String lcsql = getSqlMy(CustomerProduct.sqlId_selByCusAndType);
				CustomerProduct lccp = CustomerProduct.dao.findFirst(lcsql, customerIds, Code.lc_ids,
						getCUserOrganIds());
				if (lccp != null && lccp.getStr("proUnits").equals(Code.dlc_units)) {
					CustomerProduct dlccp = CustomerProduct.dao.findById(lccp.getStr("ids"));
					dlccp.set(CustomerProduct.column_totalExperience,
							dlccp.getFloat(CustomerProduct.column_totalExperience) - 1)
							.set(CustomerProduct.column_updateDate, new Date());
					if (!dlccp.update()) {
						log.debug("更新单疗程剩余失败");
						throw new RuntimeException("更新单疗程剩余失败!");
					}
				}
				String servicesql = getSqlMy(CustomerService.sqlId_selMinByCus);
				List<CustomerService> csList1 = CustomerService.dao.find(servicesql, customerIds);
				if (csList1 != null && csList1.size() > 0) {
					CustomerService cs1 = csList1.get(0);
					cdet.set(CustomerDetail.column_minDate, cs1.getDate(CustomerService.column_createDate));
					cdet.set(CustomerDetail.column_minWeight, cs1.getFloat(CustomerService.column_weight));
					cdet.set(CustomerDetail.column_updateDate, new Date());
					cdet.set(CustomerDetail.column_updateId, getCUserIds());
				}
				cdet.set(CustomerDetail.column_toStoreDate, new Date());
				cdet.set(CustomerDetail.column_nowWeight, nowweight);
				cdet.update();
			}
			if (flag) {
				renderText("200");
			} else {
				renderText("服务异常");
			}
		} else {
			renderText("亲，今天已经结束服务了，明天再来!");
		}
	}

	public void savePart() {
		ServicePartSize spz = new ServicePartSize();
		spz.set("customerIds", getPara("cIds"));
		spz.set("createId", getCUserIds());
		spz.set("createDate", new Date());
		spz.set("qs", getPara("posqs"));
		spz.set("qz", getPara("posqz"));
		spz.set("qx", getPara("posqx"));
		spz.set("zb", getPara("poszb"));
		spz.set("yb", getPara("posyb"));
		spz.set("zdt", getPara("poszdt"));
		spz.set("ydt", getPara("posydt"));
		spz.set("zxt", getPara("poszxt"));
		spz.set("yxt", getPara("posyxt"));
		spz.save();
		renderText("200");
	}

	/**
	 * 获取顾客减重目标
	 * 
	 */
	public void getDelweight() {
		String customerIds = getPara("customerIds");
		String date = getPara("date");
		String orgids = getCUserOrganIds();
		Map<String, Object> mm = binShopColServiceService.getDelweight(customerIds, date, orgids);
		renderJson(mm);
	}

	/**
	 * 获取顾客减重记录
	 * 
	 * @throws ParseException
	 * 
	 */
	public void getDelweiRecord() throws ParseException {
		String customerIds = getPara("customerIds");
		String date = getPara("date");
		String orgids = getCUserOrganIds();
		List<CustomerService> csLsit = binShopColServiceService.getDelweiRecord(orgids, customerIds, date);
		renderJson(csLsit);
	}

	// 体重登记 日常
	public void getEveryday() {
		String customerIds = getPara("customerIds");
		setAttr("customerIds", customerIds);
		render("dailyLife.html");
	}

	// 体重登记 日常 保存
	public void saveDaily() {
		CustomerHabit c = getModel(CustomerHabit.class);
		// 获取当前登录机构id
		String cudid = getCUserOrganIds();
		// 获取当前登陆人员id
		String userid = getCUserIds();
		c.put("organIds", cudid);
		c.put("createDate", new Date());
		c.put("createId", userid);
		boolean fag = c.save();
		if (fag) {
			renderJson("status", "200");
		} else {
			renderJson("status", "300");
		}
	}
	
	
}
